package cn.rocky.offer;

import java.util.Stack;

/**
 * @author Rocky
 * @version 1.0
 * @description 剑指offer09
 *      用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，
 *      分别完成在队列尾部插入整数和在队列头部删除整数的
 *      功能。(若队列中没有元素，deleteHead 操作返回 -1 )
 *
 * ["CQueue","appendTail","deleteHead","deleteHead"]
 * [[],[3],[],[]]
 * 输出：[null,null,3,-1]
 * ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
 * [[],[],[5],[2],[],[]]
 * 输出：[null,-1,null,null,5,2]
 *
 * @date 2021/7/10 14:03
 */
class TwoStack {
    /** stack1 入栈*/
    Stack<Integer> stack1;
    /** stack2 出栈*/
    Stack<Integer> stack2;

    public TwoStack() {
        stack1 = new Stack<Integer>();
        stack2 = new Stack<Integer>();
    }

    /**
     * 增加元素
     * @param value value
     */
    public void appendTail(int value) {
        stack1.push(value);
    }

    /**
     * 删除元素
     * @return
     */
    public int deleteHead() {
        // 如果第2个栈为空
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        if (stack2.isEmpty()) {
            return -1;
        } else {
            return stack2.pop();
        }
    }

}

/**
 * @author Rocky
 */
public class offer09 {
    public static void main(String[] args) {
        TwoStack stack = new TwoStack();
        stack.appendTail(4);
        stack.appendTail(9);
        stack.appendTail(10);
        System.out.println(stack.stack1);
        stack.deleteHead();
        System.out.println(stack.stack2);
    }
}
